package com.longrise.android.byjk.plugins.course.batchbuy;

import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.AllCourseChildBean;
import com.longrise.android.byjk.utils.TimeHelper;
import com.longrise.common.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/11/17.
 * 批量购买 右边的adapter
 */

public class BatchbuyAdapter2 extends BaseQuickAdapter<AllCourseChildBean, BaseViewHolder> {

    private OnBatchbuydetailClickListener mListener;
    private List<AllCourseChildBean> mSelectedCourseBeans = new ArrayList<AllCourseChildBean>();

    public BatchbuyAdapter2() {
        super(R.layout.item_batchbuy_rcv2, null);
    }


    public void setDatas(List<AllCourseChildBean> childBeanList) {
        setNewData(childBeanList);
    }

    /**
     * 告诉item 选择了那几个
     */
    public void setSelectedCourseBeans(List<AllCourseChildBean> selectedCourseBeans){
        mSelectedCourseBeans.clear();
        mSelectedCourseBeans.addAll(selectedCourseBeans);
        notifyDataSetChanged();
    }


    @Override
    protected void convert(BaseViewHolder helper, final AllCourseChildBean childBean) {

        final View convertView = helper.getConvertView();

        ImageView iv = helper.getView(R.id.item_batchbuy_rcv2_iv);
        //图片
        String realpic = childBean.getRealpic();
        Glide.with(mContext)
                .load(realpic)
                .placeholder(mContext.getResources().getDrawable(R.drawable.img03))
                .crossFade()
                .into(iv);
        //标题
        String name = childBean.getName();
        helper.setText(R.id.item_batchbuy_rcv2_tv1, name);
        final TextView tv_title = helper.getView(R.id.item_batchbuy_rcv2_tv1);

        tv_title.post(new Runnable() {
            @Override
            public void run() {
                int lineCount = tv_title.getLineCount();
                ViewGroup.LayoutParams layoutParams = convertView.getLayoutParams();
                if (lineCount >= 2) {
                    layoutParams.height = AppUtil.dip2px(95);
                } else {
                    layoutParams.height = AppUtil.dip2px(68);
                }
                convertView.setLayoutParams(layoutParams);
            }
        });

        //课程价格(折后价)
        String realprice = childBean.getRealprice();
        if ("0.00".equals(realprice)) {
            helper.setText(R.id.item_batchbuy_rcv2_money, "免费");
        } else {
            helper.setText(R.id.item_batchbuy_rcv2_money, "￥ " + realprice);
        }

        //课程时常
        String grouptime = childBean.getGrouptime();
        grouptime = TimeHelper.formateTime(grouptime);
        helper.setText(R.id.item_batchbuy_rcv2_time, grouptime);


        //是否被选中
        CheckBox cb = helper.getView(R.id.item_batchbuy_cb);
        if(mSelectedCourseBeans.contains(childBean)){
            cb.setChecked(true);
        }else{
            cb.setChecked(false);
        }


        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onClick(childBean);
                }
            }
        });



    }


    public void setOnItemClickListener(OnBatchbuydetailClickListener listener) {
        this.mListener = listener;
    }

    interface OnBatchbuydetailClickListener {
        void onClick(AllCourseChildBean itembean);
    }


}
